$(function () {
    $("#jqGrid").Grid({
        url: '../deviceinfo/list',
        rownumWidth: 35,
        colModel: [
            {label: '设备编码', name: 'devCode', index: 'DEV_CODE', width: 80},
            {label: '设备名称', name: 'devName', index: 'DEV_NAME', width: 80},
            {label: '大类', name: 'devTypeBName', index: 'devTypeBName', width: 80},
            {label: '中类', name: 'devTypeMName', index: 'devTypeMName', width: 80},
            {label: '小类', name: 'devTypeSName', index: 'devTypeSName', width: 80},
            {label: '小类', name: 'devTypeS', index: 'devTypeS', width: 80, hidden: true},
            {label: '生产线名称', name: 'devProductName', index: 'DEV_PRODUC_NAMET', width: 80},
            {label: '工序名称', name: 'devProceName', index: 'DEV_PROCE_NAME', width: 80},
            {label: '供应厂家', name: 'devSupplyName', index: 'DEV_SUPPLY', width: 80},
            {
                label: '设备状态', name: 'devStatus', index: 'DEV_STATUS', width: 80,
                formatter: function (v) {
                    return sysEnums.STATUS[v];
                }
            },

            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {
                label: '巡检项', name: 'option', sortable: false, align: 'center', width: 60,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.showOptionBase(\"" + row.devSupply + "\", \"" + row.devTypeS + "\")'><i class='fa fa-plus-circle'></i>&nbsp;查看</button>";
                }
            },
            {
                label: '巡检记录', name: 'option', sortable: false, align: 'center', width: 60,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.showOptionRecord(\"" + row.id + "\", \"" + row.devSupply + "\", \"" + row.devTypeS + "\")'><i class='fa fa-file-text-o'></i>&nbsp;查看</button>";
                }
            }
        ]
    });
    vm.getDevTypeB();
    vm.getTreeList();
    vm.getStore();
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    callback: {
        beforeCheck: true
    }

};
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl",
            name: "complexName"
        }
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};


function outTreeCheck(e, treeObj, treeNode) {
    vm.q.deptCode = treeObj.deptId;
    vm.query();
}

// 递归，获取所有子节点
function getAllChildrenNodes(treeNode, result) {
    if (treeNode.isParent) {
        var childrenNodes = treeNode.children;
        if (childrenNodes) {
            for (var i = 0; i < childrenNodes.length; i++) {
                result += ',' + childrenNodes[i].deptId;
                result = getAllChildrenNodes(childrenNodes[i], result);
            }
        }
    }
    return result;
}

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        deviceInfo: {
            devCode: '',
            devProductCode: '',
            devTypeB: '',//大类
            devTypeM: '',//中类
            devTypeS: '',//小类
            devSupply: '',//供应厂家
        },
        q: {
            devCode: '', // 设备编码
            devSupplyName: '', // 供应厂家名称
            deptCode: '', // 设备所处组织编码
            devTypeB: '',//大类
            devTypeM: '',//中类
            devTypeS: '',//小类

        },
        devTypeBList: [], //大类
        devTypeMList: [], //中类
        devTypeSList: [],//小类
        devChangeRecord: {
            isDel: '',
            checkReason: '',
            checkResult: '',
            checkDate: '',
            isSpec: '',
            autoNo: '',
            useDept: '',
            devProceName: '',
            devProceNo: '',
            devProductName: '',
            devStatus: '1',
            devCode: '',
            devName: '',
            id: '',
            updateDate: '',
            updateUser: '',
            createUser: '',
            createDate: ''
        },
        treeList: [],
        devStatusList: enumsToList("STATUS"),
    },
    methods: {
        query: function () {
            vm.reload();
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../deviceinfo/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.deviceInfo = r.deviceInfo;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'devCode': vm.q.devCode,
                    'deptCode': vm.q.deptCode,
                    'devTypeB': vm.q.devTypeB,
                    'devTypeM': vm.q.devTypeM,
                    'devTypeS': vm.q.devTypeS,
                    'devSupplyName': vm.q.devSupplyName
                },
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            // 取消选中
            deptZtree.cancelSelectedNode();
            // 重置分页
            $("#jqGrid").jqGrid('setGridParam', {
                page: 1
            });
            vm.q = {
                devCode: '',
                deptCode: '',
                devTypeB: '',//大类
                devTypeM: '',//中类
                devTypeS: '',//小类
                devSupplyName: '',//供应厂家
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getDevTypeB: function () {
            Ajax.request({
                url: "../devtype/queryAll",
                params: {typeTp: 2},
                async: true,
                successCallback: function (r) {
                    vm.devTypeBList = r.list;
                }
            });
        },
        getDevTypeM: function (id) {
            if (id) {
                Ajax.request({
                    url: "../devtype/queryAll",
                    params: {typePid: id, typeTp: 1},
                    async: true,
                    successCallback: function (r) {
                        vm.devTypeMList = r.list;
                    }
                });
            }
        },
        getDevTypeS: function (id) {
            if (id) {
                Ajax.request({
                    url: "../devtype/queryAll",
                    params: {typePid: id, typeTp: 0},
                    async: true,
                    successCallback: function (r) {
                        vm.devTypeSList = r.list;
                    }
                });
            }
        },
        getTreeList: function () {
            Ajax.request({
                url: '../sys/user/treeDeptBySCX',
                async: false,
                successCallback: function (r) {
                    vm.treeList = r.list;
                }
            });
        },
        //加载列表页树
        getStore: function () {
            deptZtree = $.fn.zTree.init($("#storeTree"), deptSetting, vm.treeList);
            // 默认展开工厂一级
            deptZtree.expandNode(deptZtree.getNodeByParam("deptId", '01', null));
        },
        showOptionBase: function (faCode, typeCode) {
            //关联巡检选项
            openWindow({
                type: 2,
                area:['100%','100%'],
                title: '<i class="fa fa-bars"></i> 关联巡检选项',
                content: '../device/InspectionRecord.html?typeCode=' + typeCode + "&faCode=" + faCode
            });
        },
        showOptionRecord: function (devId, faCode, typeCode) {

            //判断当前设备是否有巡检计划
            Ajax.request({
                url: '../inspectionoption/queryAll',
                params: {"faCode": faCode, 'typeCode': typeCode},
                contentType: "application/json",
                async: false,
                successCallback: function (res) {
                    var num = res.num;
                    if (num > 0) {
                        //跳转巡检设备列表
                        openWindow({
                            type: 2,
                            title: '<i class="fa fa-bars"></i> 巡检记录日历',
                            content: '../device/devFollow.html?devId=' + devId
                        });
                    } else {
                        alert('暂无巡检项！');
                    }
                }
            });
        }
    }
});
